package NIO;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 47868
 */
public class BufferTest1 {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(28);
        buffer.put("hello world\ni'm ok\nlet's go\n".getBytes());
        List<ByteBuffer> byteBuffers= print(buffer);
        for (ByteBuffer b : byteBuffers) {
            while (b.hasRemaining()) {
                System.out.print((char)b.get());
            }
        }
    }
    public static List<ByteBuffer> print(ByteBuffer buffer) {
        List<ByteBuffer> list = new ArrayList<>();
        buffer.flip();
        for(int i=0;i<buffer.limit();i++){
            if(buffer.get(i)=='\n'){
                int length=i+1-buffer.position();
                ByteBuffer newBuffer = ByteBuffer.allocate(length);
                for(int j=0;j<length;j++){
                    newBuffer.put(buffer.get());
                }
                newBuffer.flip();
                list.add(newBuffer);
            }
        }
        return list;
    }
}
